package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityPeriodBinding;
import com.apex.aolonfitdemo.dialog.AgeDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.PeriodCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.model.EABlePeriod;
import com.apex.ax_bluetooth.model.EABlePhysiologyData;
import com.apex.ax_bluetooth.utils.LogUtils;
import com.cyber.bridge.module.bean.MenstrualCycleEntity;
import com.cyber.bridge.module.listener.OnCyberQueryAndSetStateListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;


public class PeriodActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();

    private WaitingDialog waitingDialog;
    private AgeDialog ageDialog;
    private int tempRest;


    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                MenstrualCycleEntity menstrualCycleEntity = (MenstrualCycleEntity) msg.obj;
                if (menstrualCycleEntity == null) {
                    return;
                }
                activityPeriodBinding.cycle.setText(menstrualCycleEntity.getMenstrualCycleDays() + "");
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(menstrualCycleEntity.getLastMenstrualDate());
                activityPeriodBinding.startTime.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.YEAR));
                activityPeriodBinding.duration.setText(menstrualCycleEntity.getMenstrualPeriodDays() + "");
            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(PeriodActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_MONTH,-5);
                activityPeriodBinding.startTime.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.YEAR));
                activityPeriodBinding.duration.setText(7 + "");
                activityPeriodBinding.cycle.setText(28 + "");
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(PeriodActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityPeriodBinding activityPeriodBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityPeriodBinding = ActivityPeriodBinding.inflate(getLayoutInflater());
        setContentView(activityPeriodBinding.getRoot());
        activityPeriodBinding.tool.setNavigationIcon(com.apex.aolonfitdemo.R.mipmap.exit_page);
        setSupportActionBar(activityPeriodBinding.tool);
        getSupportActionBar().setTitle(null);
        activityPeriodBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnMenstrualCycleListener(new OnCyberQueryAndSetStateListener<MenstrualCycleEntity>() {
            @Override
            public void queryConfig(MenstrualCycleEntity menstrualCycleEntity) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.obj = menstrualCycleEntity;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void setConfig(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }

            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(PeriodActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().queryMenstrualCycleInfo();

        //For example, the default current time is the start time of physiological period,
        // the duration of physiological period is 7 days, and the interval between physiological periods is 28 days
        activityPeriodBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(PeriodActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_MONTH, -5);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                long periodStartTime = calendar.getTimeInMillis();
                int keepTime = 5;
                int cycleTime = 30;
                AolonFitManager.getInstance().setMenstrualCycleInfo(new MenstrualCycleEntity(periodStartTime, keepTime, cycleTime, null));

            }
        });

    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (ageDialog != null) {
            ageDialog.dismiss();
            ageDialog.destroyDialog();
            ageDialog = null;
        }
        super.onDestroy();
    }
}
